package com.bw.zg3park2212.news

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.bw.base.entity.NewsEntity
import com.bw.base.entity.NewsEntityItem
import com.bw.base.net.ApiService
import com.bw.base.net.Res

class NewsSocuer(val apiService: ApiService):PagingSource<Int,NewsEntityItem>() {
    override fun getRefreshKey(state: PagingState<Int, NewsEntityItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NewsEntityItem> {
        try {
            val page=params.key?:1
           // var res=apiService.getnews()
            var res:Res<NewsEntity> ?=null

                 res=apiService.getnews()

//            val prePage=if (page!=1) page-1 else null
//            val nextPage=if (res?.data?.size!=0) page+1 else null
            return LoadResult.Page(res!!.data.sortedByDescending { it.publishDate },null,null)
        }catch (e:Exception){
            return LoadResult.Error(e)
        }
    }
}